//
//  MYGChangeEnterMessageImageCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/10.
//

import UIKit
import TZImagePickerController

class MYGChangeEnterMessageImageCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChangeEnterMessageImageCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            if model.image != nil {
                avatar.setImage(model.image, for: .normal)
            } else if model.url != nil {
                avatar.sd_setImage(with: URL(string: "\(model.url ?? "http://followup-img.oss-cn-beijing.aliyuncs.com/imgs/718b9d8a6e0b02995b75948719f86a48")"), for: .normal)

            } else {
                avatar.setImage(UIImage(named: "head"), for: .normal)
            }
        }
    }
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
        
        let tap = UITapGestureRecognizer(target: self, action:  #selector(selectImage))
        addGestureRecognizer(tap)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(nameLabel)
        contentView.addSubview(avatar)
        contentView.addSubview(lineView)
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.centerY.equalToSuperview()
        }
        
        avatar.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(36)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    // MARK: - lazy load
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(51, 51, 51, 1)
        label.font = .regular(16)
        label.text = "头像"
        return label
    }()
    
    private lazy var avatar: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "head"), for: .normal)
        btn.layer.cornerRadius = 25
        btn.layer.masksToBounds = true
        btn.isUserInteractionEnabled = false
//        btn.addTarget(self, action: #selector(selectImage), for: .touchUpInside)
        return btn
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
}

extension MYGChangeEnterMessageImageCell: TZImagePickerControllerDelegate {
    @objc
    private func selectImage() {
        let pick = TZImagePickerController(maxImagesCount: 1, delegate: self)
        
        UIViewController.currentViewController?.present(pick!, animated: true)
    }
    
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        guard let image = photos.first else {
            return
        }
        
        model?.image = image
        avatar.setImage(image, for: .normal)
    }
}
